package com.lf.web.action.system;

import com.lf.data.WebNodeOption;
import com.lf.data.Cluster;
import com.lf.data.check.WebNodeOptionCheck;
import com.lf.data.dao.WebNodeOptionDAO;
import com.lf.data.search.Query;
import com.lf.data.search.Sort;
import com.lf.web.service.ResponseCode;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class WebNodeOptionService extends ActionSupport implements ServletResponseAware {

    private HttpServletResponse res;
    private static final Logger LOGGER = Logger.getLogger(WebNodeOptionService.class);
    private WebNodeOptionCheck check = new WebNodeOptionCheck();
    private WebNodeOptionDAO channeloptiondao;
    private List<Cluster> rslist = null;

    private String updateName = "";
    private String updateValue = "";

    private WebNodeOption channeloption;

    private String id = "";
    private String name = "";
    private String description = "";
    private String channelid = "";
    private int index = 0;
    private String type = "";
    private String list = "0";

    public void setChanneloptiondao(WebNodeOptionDAO channeloptiondao) {
        this.channeloptiondao = channeloptiondao;
    }

    @Override
    public void setServletResponse(HttpServletResponse res) {
        this.res = res;
    }

    public void setId(String id) {
        if (check.checkId(id))
            this.id = id;
        else
            this.id = "";
    }

    public void setChannelid(String channelid) {
        this.channelid = channelid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setList(String list) {
        this.list = list;
    }

    public void setUpdatename(String updateName) {
        this.updateName = updateName;
    }

    public void setUpdatevalue(String updateValue) {
        this.updateValue = updateValue;
    }

    public String execute() throws IOException {
        if (check.checkWebNodeid(channelid)) {
            WebNodeOption co = new WebNodeOption();
            Query query = new Query();
            query.eq(co.WebNodeId, channelid);
            Sort sort = new Sort();
            sort.desc(co.Index);
            rslist = channeloptiondao.find(query, null, sort);
            return SUCCESS;
        } else {
            LOGGER.error("非法查询ChannelOption：channelid=" + channelid);
            res.sendError(404);
            return NONE;
        }
    }

    public void addAction() throws IOException {
        if (check.checkWebNodeid(channelid) && check.checkName(name) && check.checkIndex(index) && check.checkType(type) && check.checkList(list)) {
            channeloption = new WebNodeOption();
            id = channeloption.setId();
            channeloption.put(channeloption.WebNodeId, channelid);
            channeloption.put(channeloption.Name, name);
            channeloption.put(channeloption.Description, description);
            channeloption.put(channeloption.Type, type);
            channeloption.put(channeloption.List, list);
            channeloption.put(channeloption.Index, index);
            channeloptiondao.insert(channeloption);
            JSONObject json = new JSONObject();
            json.put("code", ResponseCode.FINISH);
            json.put("msg", "添加成功！");
            json.put("id", id);
            res.getWriter().write(json.toString());
        } else {
            res.sendError(404);
            LOGGER.info("非法添加模块选项：channelid=" + channelid + ",name=" + name + ",index=" + index + ",type=" + type);
        }
    }

    public void save() throws IOException {
        try {
            if (check.checkUpdateName(updateName) && !id.equals("") && check.doCheck(check, updateName, updateValue)) {
                int rs = 0;
                if ("index".equals(updateName)) {
                    try {
                        int v = Integer.parseInt(updateValue);
                        //rs = channeloptiondao.update(id, updateName, v);
                    } catch (Exception e) {
                        rs = -1;
                    }
                } else {
                    //rs = channeloptiondao.update(id, updateName, updateValue);
                }
                JSONObject json = new JSONObject();
                json.put("code", rs);
                if (rs != 1) {
                    json.put("msg", "保存失败！");
                } else {
                    json.put("msg", "保存成功！");
                }
                res.getWriter().write(json.toString());
            } else {
                LOGGER.error("非法参数：id=" + id + ",updateName=" + updateName + ",updateValue=" + updateValue);
                res.sendError(404);
            }
        } catch (Exception e) {
            LOGGER.error("异常参数：id=" + id + ",updateName=" + updateName + ",updateValue=" + updateValue + ":" + e.getMessage());
            res.sendError(404);
        }
    }

    public void delete() throws IOException {
        if (!id.equals("")) {
            int rs = channeloptiondao.removeById(id);
            JSONObject json = new JSONObject();
            json.put("code", rs);
            res.getWriter().write(json.toString());
        } else {
            LOGGER.error("非法删除模块选项：id=" + id);
            res.sendError(404);
        }
    }

    public List<Cluster> getRslist() {
        return rslist;
    }

    public WebNodeOptionCheck getCheck() {
        return check;
    }

    public String getChannelid() {
        return channelid;
    }
}
